#include <stdio.h>
#include <unistd.h>

#include "fpga/show_v7.h"
#include "adc/i2c_fp.h"
#include "adc/bit_adc.h"
#include "global/gpio.h"
#include "utility/testfun.h"

void ShowTab()
{
	int _usec =8000;

	printf ("\r\n------------DBF-304-BOARD-MENU-LIST--------------\r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(1)	CPU INFO \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(2)	FLASH WRITE TEST   \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(3)	NET TEST  \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(4)	AXI I2C  \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(5)	MEMORY TEST   \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(6)	BIT INFO   \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(7)	V7 DDR TEST    \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(8)	RST SERDES STATUS     \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(9)	V7 SERDES STATUS       \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(0)	V7 FLASH TEST       \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(a)	SERDES RECV         \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(b)	ZYNQ SERDES SEND TO V7 TEST        \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(c)	RESET V7        \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(d)	CLOSE FP-1        \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(e)	CLOSE FP-2        \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(f)	CLOSE FP-3        \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(g)	CLOSE FP-4        \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(h)	CLOSE FP-5        \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(i)	CONFIG FP USING POB.TXT        \r\n");
	usleep(_usec);
	printf("\r\nKeyboard-(q)	QUIT     \r\n");
	usleep(_usec);
	printf("-----------------------------------------------------------------------------------------------\r\n");
	usleep(_usec);
}


void SlectUartMenu(void)
{
	unsigned char _recvData;
	unsigned int _bRunning;
	_recvData = '0';
	_bRunning = 1;
	sleep(1);
	ShowTab();

	while(_bRunning)
	{
		_recvData = getchar();
		if (_recvData == '1')
		{
			printf("\n*****CPU INFO*****\r\n");
			cpuinfo();
		}
		else if (_recvData == '2')
		{
			printf("\n*****FLASH WRITE TEST*****\r\n");
			flashTest();
		}
		else if (_recvData == '3')
		{
			printf("\n*****NET TEST*****\r\n");
			netinfo();
		}
		else if (_recvData == '4')
		{
			printf("\n*****AXI I2C*****\r\n");
			read_fp_temp();
		}
		else if (_recvData == '5')
		{
			printf("\n*****MEMORY TEST*****\r\n");
			memTest();
		}
		else if (_recvData == '6')
		{
			printf("\n*****BIT INFO*****\r\n");
			Show_bit();
		}
		else if (_recvData == '7')
		{
			printf("\n*****V7 DDR TEST*****\r\n");
			SerdesSpaceInit();
			Sertest();
//			SerRecV();
//			SerSend();
			sleep(1);
			SerdesSpaceummap();
		}
		else if (_recvData == '8')
		{
			printf("\n*****RST SERDES STATUS*****\r\n");
			SerdesSpaceInit();
			start_serdes();
			sleep(1);
			SerdesSpaceummap();
		}

		else if (_recvData == '9')
		{
			printf("\n*****V7 SERDES STATUS*****\r\n");
			show_V7_info();
		}
		else if (_recvData == '0')
		{
			printf("\n*****V7 FLASH TEST*****\r\n");
			FlashTest();
		}
		else if (_recvData == 'a')
		{
			printf("\n*****SERDES RECV*****\r\n");
//			SerdesSpaceInit();
//			int a = GetGpioReg(GPIO_V_1,0x0);
//			int b = GetGpioReg(GPIO_V_2,0x0);
//			int c = GetGpioReg(GPIO_V_3,0x0);
//			SetGpioReg(GPIO_V_1,0x0,~a);
//			SetGpioReg(GPIO_V_1,0x0,~b);
//			SetGpioReg(GPIO_V_1,0x0,~c);
//			SerdesSpaceummap();
		}
		else if (_recvData == 'b')
		{
			printf("\n*****ZYNQ SERDES SEND TO V7 TEST*****\r\n");
			SerSend();
		}
		else if (_recvData == 'c')
		{
			printf("\n*****RESET V7*****\r\n");
			reset_v7();
		}
		else if (_recvData == 'd')
		{
			printf("\n*****CLOSE FP-1*****\r\n");
			close_fp(1);
		}
		else if (_recvData == 'e')
		{
			printf("\n*****CLOSE FP-2*****\r\n");
			close_fp(2);
		}
		else if (_recvData == 'f')
		{
			printf("\n*****CLOSE FP-3*****\r\n");
			close_fp(3);
		}
		else if (_recvData == 'g')
		{
			printf("\n*****CLOSE FP-4*****\r\n");
			close_fp(4);
		}
		else if (_recvData == 'h')
		{
			printf("\n*****CLOSE FP-5*****\r\n");
			close_fp(5);
		}
		else if (_recvData == 'i')
		{
			printf("\n*****CONFIG FP USING POB.TXT*****\r\n");
			config_fp();
		}
		else if (_recvData == 'q')
		{
			printf("\n*****WARNNING!! QUIT THE MENU*****\r\n");
			_bRunning = 0;
			break;
		}
		else if(_recvData == '\n'||_recvData == '\r')
		{
			printf("->");
		}
	}
}
